package com.scud.product.controller;

import com.alibaba.fastjson.JSON;
import com.scud.product.service.ProductService;
import com.scud.common.result.Result;
import com.scud.model.PageResult;
import com.scud.model.product.dto.ProductPageDto;
import com.scud.model.product.dto.SpecsSelectedDto;
import com.scud.model.product.pojo.Product;
import com.scud.model.product.pojo.SpecSelected;
import com.scud.model.product.vo.ProductDetailsVo;
import com.scud.model.product.vo.ProductSkuVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/product/product")
@Slf4j
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 根据商品id获取商品详情
     *
     * @param id
     * @return
     */
    @GetMapping("/getDetails/{id}")
    public Result<ProductDetailsVo> getProductDetails(@PathVariable Long id) {
        ProductDetailsVo product = productService.getProductDetails(id);
        return Result.success(product);
    }

    /**
     * 根据分类id条件分页查询商品
     *
     * @param categoryId
     * @return
     */
    @PostMapping("/pageByCateId/{categoryId}")
    public Result<PageResult<Product>> pageByCateId(@PathVariable Integer categoryId,
                                                    @RequestBody ProductPageDto productPageDto) {
        PageResult<Product> res = productService.pageByCateId(categoryId, productPageDto);
        return Result.success(res);
    }

    /**
     * 随机获取n件商品，返回当做轮播图
     * @param :
     * @return Result
     * @author vermouth
     */
    @GetMapping("/getBanner")
    public Result getBanner(){
        log.info("随机获取n件商品，返回当做轮播图");
        List<Product> bannerList=productService.getBanner();
        return Result.success(bannerList);
    }

    /**
     * 获取一天内的新商品
     * @return
     */
    @GetMapping("/getNew")
    public Result<List<Product>> getNew(){
        log.info("获取一天内的新商品");
        List<Product> newList = productService.getNew();
        return Result.success(newList);
    }

    /**
     * 获取首页最新商品
     * @return
     */
    @GetMapping("/homeNew")
    public Result<List<Product>> homeNew(){
        log.info("获取首页最新商品");
        List<Product> newList = productService.homeNew();
        return Result.success(newList);
    }

    /**
     * 分页查询所有商品
     * @param dto:
     * @return Result
     * @author vermouth
     */
    @PostMapping("/searchProduct")
    public Result searchProduct(@RequestBody ProductPageDto dto){
        log.info("分页查询所有商品");
        PageResult<Product> pageResult =productService.searchProduct(dto);
        return Result.success(pageResult);
    }

    /**
     * 获取商品Sku的详情
     * @param productId
     * @param specs
     * @return 若数据库不存在该sku，则返回null
     */
    @PostMapping("/getSku/{productId}")
    public Result<ProductSkuVo> getSku(@PathVariable Long productId,
                                       @RequestBody List<SpecSelected> specs){
        log.info("获取商品Sku的详情");
        ProductSkuVo skuVo = productService.getSku(new SpecsSelectedDto(productId, JSON.toJSONString(specs)));
        return Result.success(skuVo);
    }
}
